package com.cw.jeeyt.controller.api;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.api.ApiFacadeDocBo;
import com.cw.jeeyt.service.bo.api.ApiFacadeMethodBo;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.service.intf.api.ApiFacadeDocService;
import com.cw.jeeyt.service.intf.api.ApiFacadeMethodService;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * api接口方法控制层
 *
  * @author chenw
 * @version 1.0.0
 */
@Controller
@RequestMapping("/apiFacadeMethod")
public class ApiFacadeMethodController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(ApiFacadeMethodController.class);
    @Resource
    private ApiFacadeMethodService apiFacadeMethodService;
    @Resource
    private ApiFacadeDocService apiFacadeDocService;

    /**
     * 跳转到api接口方法列表查询页面
     *
     * @param apiFacadeMethodBo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query(String fromPage, ApiFacadeMethodBo apiFacadeMethodBo) throws Exception {

        ModelAndView mv = new ModelAndView("api/apiFacadeMethod_index");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("docId", apiFacadeMethodBo.getDocId());
        mv.addObject("fromPage", fromPage);
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listApiFacadeMethod")
    public ResultData listApiFacadeMethod(@RequestBody DataTable dataTable, Long docId) throws Exception {

        ApiFacadeMethodBo apiFacadeMethodBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            ApiFacadeMethodBo.class);
        if (apiFacadeMethodBo == null) {
            apiFacadeMethodBo = new ApiFacadeMethodBo();
        }
        apiFacadeMethodBo.setDocId(docId);
        PageInfo<ApiFacadeMethodBo> pageInfo = apiFacadeMethodService.queryByPage(apiFacadeMethodBo,
            dataTable.getStart(), dataTable.getLength(), dataTable.getOrder()[0]);
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到api接口方法表单页面
     *
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-tab")
    public ModelAndView toTab(Long id, String operation, Long docId, String fromPage)
    throws Exception {

        ModelAndView mv = new ModelAndView("api/apiFacadeMethod_tab");
        ApiFacadeMethodBo apiFacadeMethodBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            apiFacadeMethodBo = new ApiFacadeMethodBo();
            apiFacadeMethodBo.setDocId(docId);
            ApiFacadeDocBo apiFacadeDoc = apiFacadeDocService.getApiFacadeDocById(docId);
            if (apiFacadeDoc != null) {
                apiFacadeMethodBo.setApiFacadeCode(apiFacadeDoc.getApiFacadeCode());
                apiFacadeMethodBo.setApiFacadeName(apiFacadeDoc.getApiFacadeName());
            }
        } else {
            apiFacadeMethodBo = apiFacadeMethodService.getApiFacadeMethodById(id);
        }
        mv.addObject("apiFacadeMethod", apiFacadeMethodBo);
        mv.addObject("operation", operation);
        mv.addObject("fromPage", fromPage);
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 跳转到api接口方法表单页面
     *
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView editForm(Long id, String operation, Long docId, String fromPage)
    throws Exception {

        ModelAndView mv = new ModelAndView("api/apiFacadeMethod_form");
        ApiFacadeMethodBo apiFacadeMethodBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            apiFacadeMethodBo = new ApiFacadeMethodBo();
            apiFacadeMethodBo.setDocId(docId);
            ApiFacadeDocBo apiFacadeDoc = apiFacadeDocService.getApiFacadeDocById(docId);
            if (apiFacadeDoc != null) {
                apiFacadeMethodBo.setApiFacadeCode(apiFacadeDoc.getApiFacadeCode());
                apiFacadeMethodBo.setApiFacadeName(apiFacadeDoc.getApiFacadeName());
            }
        } else {
            apiFacadeMethodBo = apiFacadeMethodService.getApiFacadeMethodById(id);
        }
        mv.addObject("apiFacadeMethod", apiFacadeMethodBo);
        mv.addObject("operation", operation);
        mv.addObject("fromPage", fromPage);
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 根据操作类型保存api接口方法数据
     *
     * @param apiFacadeMethodBo
     * @param operation add 新增  edit 修改
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/saveApiFacadeMethod", method = RequestMethod.POST)
    public JsonResultDto saveApiFacadeMethod(ApiFacadeMethodBo apiFacadeMethodBo,
        String operation) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                apiFacadeMethodService.insert(apiFacadeMethodBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                apiFacadeMethodService.update(apiFacadeMethodBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            jsonResultDto.setMsg("保存失败:" + e.getMessage());

            e.printStackTrace();
            logger.error("保存api接口方法失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 删除api接口方法
     *
     * @param apiFacadeMethodBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto delete(ApiFacadeMethodBo apiFacadeMethodBo) throws Exception {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            apiFacadeMethodService.delete(apiFacadeMethodBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("删除失败：" + e.getMessage());
            logger.error("删除api接口方法失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
